<?php
/**
 * Created by PhpStorm.
 * User: liuda
 * Date: 2019/2/22
 * Time: 15:15
 */

namespace app\h5\controller;


use app\common\cache\ProductCache;
use app\common\cache\SmsCache;
use app\common\helper\UserHelper;
use app\common\model\Product;
use app\common\model\ProductData;
use app\common\model\User;
use app\common\service\ChannelService;
use app\common\service\DataService;
use app\common\utils\RedisCache;
use app\h5\business\ItemBusiness;
use think\Db;
use think\facade\Session;

class Home extends Base
{
    public function index()
    {
        $this->checkLogin();
        $bannerList = [
            [
                'image' => 'http://xianjinpu.oss-cn-hangzhou.aliyuncs.com/lyfte1541042347388.png',
                'url' => 'http://www.baidu.com',
            ],
            [
                'image' => 'http://xianjinpu.oss-cn-hangzhou.aliyuncs.com/mdg2w1541042094655.png',
                'url' => 'http://v.qq.com',
            ],
        ];
        $newItemList = ItemBusiness::getInstance()->getNewItemList();
        $hotItemList = ItemBusiness::getInstance()->getHotItemList();
        $this->assign('newItemList', $newItemList);
        $this->assign('hotItemList', $hotItemList);
        $this->assign('bannerList', $bannerList);
        return $this->fetch();
    }

    public function jumpItem()
    {
        $this->checkLogin();
        $id = $this->request->get('id');
        if (!$id) {
            $this->error('产品不存在');
            exit;
        }
        $item = ItemBusiness::getInstance()->getItemById($id);
        if(!$item){
            $this->error('产品不存在');
            exit;
        }
        $uid = Session::get('uid');
        if(!ProductCache::userHasClickProductCache($id,$uid)){
            ProductCache::setProductClickCache($id, $uid);
            ProductCache::incrProductClickCache($id);
            $model = new Product();
            $product = $model->get($id);
            $hasReduce = DataService::checkHasReduce($this->loginUser['channel_id']);
            $data = [
                'product_id' => $id,
                'user_id' => $uid,
                'channel_id' => $this->loginUser['channel_id'],
                'date' => date('Ymd', time())
            ];
            //是否需要扣量
            if(!$hasReduce){
                $product->setInc('clickCount',1);
                $data['is_reduce'] = 1;
            }

            $productData = new ProductData();
            $productData->save($data);
        }
        $this->redirect($item['link']);
    }

    public function login()
    {
        $redirectUrl = $this->request->param('redirectUrl');
        $code = $this->request->param('code');
        $this->assign('redirectUrl', $redirectUrl);
        $this->assign('code',$code);
        return $this->fetch();
    }

    public function doLogin(){
        $mobile = $this->request->param('mobile');
        $smsCode = $this->request->param('sms_code');
        $code = $this->request->param('code','');
        if(empty($mobile)){
            _failure('请输入手机号');
        }
        if(!UserHelper::checkMobile($mobile)){
            _failure('手机号码格式不正确');
        }
        if($smsCode == ''){
            _failure('请填写验证码');
        }
        if(!RedisCache::addLock('h5_login_'.$mobile,2)){
            _failure('请稍后重试');
        }
        $sms = SmsCache::getMobileSmsCode($mobile);
        if($sms != $smsCode){
            _failure('验证码不正确');
        }
        $model = new User();
        $user = $model->where(['mobile'=>$mobile])->find();

        if(!$user){
            $channel = ChannelService::getChannelByCode($code);
            $channelId = 0;
            $isReduce = 0;
            Db::startTrans();
            $last1 = true;
            $data = ['mobile'=>$mobile];
            if($channel){
                //检测是否被扣量
                $hasReduce = DataService::checkUserHasReduce($channel['id']);
                if($hasReduce){
                    $isReduce = 1;
                }
                $last1 = $channel->setInc('register_count',1);
                $channelId = $channel['id'];
            }
            $data['channel_id'] = $channelId;
            $data['is_reduce'] = $isReduce;
            $last = $model->save($data);
            if(!$last || !$last1){
                Db::rollback();
                _failure('登录失败');
            }
            Db::commit();
            $userId = $model->id;

        }else{
            $userId = $user->id;
        }

        session('uid',$userId);
        _success('登录成功');

    }

    public function my()
    {
        $user = User::get(Session::get('uid'));
        $this->assign('info', $user);
        return $this->fetch();
    }
}